/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Lenovo
 * Date: 2025-08-16
 * Time: 18:11
 */
//https://leetcode.cn/problems/minimum-operations-to-reduce-x-to-zero/
public class Test {
    public static int minOperations(int[] arr, int x) {
        int size=arr.length;
        int p1=0,p2=size;
        int sum=0;
        int min=size+1;
        while(sum<x){
            p2--;
            if(p2<0){
                return -1;
            }
            sum+=arr[p2];
        }
        if(sum==x){
            min=size-p2;
        }
        while(p2<size&&p1<=p2){
            if(sum>x){
                sum-=arr[p2++];
            }
            else if(sum<x){
                sum+=arr[p1++];
            }
            else{
                min=Math.min(min,p1+size-p2);
                sum-=arr[p2++];
                sum+=arr[p1++];
            }
        }
        sum=0;p1=0;
        while(sum<x){
            sum+=arr[p1++];
        }
        if(sum==x){
            min=Math.min(min,p1);
        }
        return min>size?-1:min;
    }
    public static void main(String[] args) {
        int[] arr={3,2,20,1,1,3};
        System.out.println(minOperations(arr,10));
    }
}
